<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!--
            row：可以用栅格布局和弹性布局,type='flex'即为弹性布局
            col: 设置span写占比，span=24时候为占满
         -->
        <tree-tools :tree-node="company" :is-root="true" @addDept="addDept" />
      </el-card>
      <!-- 树形结构：节点名称+子节点 -->
      <!-- el-tree 节点名称：label 子节点：children -->
      <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
        <!--
            row：可以用栅格布局和弹性布局,type='flex'即为弹性布局
            col: 设置span写占比，span=24时候为占满
         -->
        <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDept="addDept" @editDept="editDept" />
      </el-tree>
    </div>
    <!-- tree-node: 是点击添加子部门的父节点 -->
    <addDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import treeTools from './components/tree-tools.vue'
import { listToTree } from '@/utils'
import addDept from './components/add-dept.vue'

export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      company: { },
      departs: [],
      defaultProps: { label: 'name' },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    addDept(node) {
      this.showDialog = true
      this.node = node
    },
    editDept(node) {
      this.showDialog = true
      this.node = node // 记录当前节点 异步更新
      //   获取详情
      // 更新add-dept 里面formData属性
      this.$nextTick(() => {
        this.$refs.addDept.getDepartDetail() // 异步动作
      })
    //   this.$refs.addDept.getDepartDetail(node)
    },
    async  getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: result.companyManage, id: '' }
      this.departs = listToTree(result.depts, '')
    }
  }
}
</script>

<style>

</style>
